package com.qf.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 字符串密码 -> 32位MD5字符串
 */
public class MD5Utils {

    public static String str2MD5(String path) {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(path.getBytes(StandardCharsets.UTF_8));
            byte[] b = md.digest();
            int d;
            for (byte value : b) {
                d = value;
                if (d < 0) {
                    d = value & 0xff;
                    // 与上一行效果等同
                    // i += 256;
                }
                if (d < 16)
                    sb.append("0");
                sb.append(Integer.toHexString(d));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
